package com.ease.map.layer {
    import com.ease.map.Map;
    import com.ease.map.geometry.basetype.Extent;
    import com.ease.map.geometry.basetype.Location;
    import com.ease.map.geometry.basetype.Pixel;
    import com.ease.map.geometry.basetype.Size;
    import com.ease.map.tile.ImageTile;
    import com.ease.map.layer.Grid;
    import com.ease.map.layer.Layer;

    /**
     * The Tiled Web Service provides access to resources, in particular, to rendered
     * cartographic tiles at fixed scales.
     */
    public class TMS extends Grid {
        private var _serviceVersion:String = "1.0.0";

        private var _tileOrigin:Location = null;

        private var _format:String = "png";

        /**
         * A list of all resolutions available on the server.
         * Only set this property if the map resolutions differs from the server
         */
        private var _serverResolutions:Array = null;

        public function TMS(id:int, name:String, url:String) {
            super(id, name, url);
            this.singleTile = false;
            this.xBuffer = 2;
            this.yBuffer = 1;
            this.CACHE_SIZE = 256;
            this.tweenOnZoom = true;
        }

        override public function getURL(bounds:Extent):String {
            var res:Number = this.map.resolution;
            if (this._tileOrigin == null) {
                this._tileOrigin = new Location(this.map.maxExtent.xmin, this.map.maxExtent.ymax);
            }

            var x:Number = Math.round((bounds.xmin - this._tileOrigin.lon) / (res * this.tileWidth));
            //var y:Number = Math.round((bounds.bottom - this._tileOrigin.lat) / ( res* this.tileHeight));
            var y:Number = Math.round((this._tileOrigin.lat - bounds.ymax) / (res * this.tileHeight)); //modified by hongfly

            var z:Number = (this._serverResolutions != null) ? this._serverResolutions.indexOf(res) : this.map.zoom;

            //var url:String = this.url + this._serviceVersion +"/" + this.layerName + "/" + z + "/" + x + "/" + y+"."+this._format;
            var url:String = this.url + "/" + z + "/" + y + "/" + z + "_" + y + "_" + x + "." + this._format;

            if (z < 0 || y < 0 || x < 0 || y >= Math.pow(2, z) || x >= Math.pow(2, z)) {
                //错误的url
                return null;
            }

            return url;
        }

        override public function addTile(bounds:Extent, position:Pixel):ImageTile {
            return new ImageTile(this, position, bounds, this.getURL(bounds), new Size(this.tileWidth, this.tileHeight));
        }

        override public function set map(map:Map):void {
            super.map = map;
            if (!this._tileOrigin) {
                this._tileOrigin = new Location(this.map.maxExtent.xmin, this.map.maxExtent.ymax);
            }
        }

        /**
         * setter for tile image format
         *
         * @param value:String the tile image extention
         */
        public function set format(value:String):void {
            if (value.length == 0)
                return;
            else if (value.charAt(0) == '.')
                this._format = value.substr(1, value.length - 1);
            else
                this._format = value;
        }

        /**
         * getter for tile image format
         *
         * @return String the tile image format
         */
        public function get format():String {
            return this._format;
        }

        /**
         * setter and getter of the TMS grid origin
         */
        public function set tileOrigin(value:Location):void {
            this._tileOrigin = value;
        }

        public function get tileOrigin():Location {
            return this._tileOrigin.clone();
        }

        override public function clone():Layer {
            var tms:TMS = new TMS(this.id, this.name, this.url);
            tms.tileOrigin = (this.tileOrigin == null) ? null : this.tileOrigin.clone();
            return tms;
        }

    }
}

